package com.blt.car.me;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseActivity;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.login.InitActivity;
import com.blt.car.login.LoginActivity;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.login.LoginApi;


/**
 * Created by xgp on 2017/6/20.
 */

public class SettingActivity extends BaseTitleBarActivity {
    public static final String OPEN_ACTIVITY_TYPE = "open_activity_type";

    public static final int USER_GUIDE = 1;
    public static final int INVITE_FRIEND = 2;
    public static final int ABOUT_US = 3;


    private ListView setListview;

    private String itemNames[];

    int activityType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initData() {
        super.initData();
        itemNames = getResources().getStringArray(R.array.set_item);
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("设置");
        holder.get(R.id.setting_logout);
        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CoreManager.getCore(LoginApi.class).logout();
            }
        }, R.id.setting_logout);


        setListview = holder.get(R.id.setting_list_view);
        setListview.setAdapter(new SetAdapter());

        setListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        openActivity(ModifyActivity.class);
                        break;
                    case 1:
                        openActivity(UserGuideActivity.class, OPEN_ACTIVITY_TYPE, USER_GUIDE);
                        break;
                    case 2:
                        openActivity(UserGuideActivity.class, OPEN_ACTIVITY_TYPE, INVITE_FRIEND);
                        break;
                    case 3:
                        openActivity(FeedBackActivity.class);
                        break;
                    case 4:
                        openActivity(UserGuideActivity.class, OPEN_ACTIVITY_TYPE, ABOUT_US);
                        break;
                    case 5:
                        openActivity(TestApiActivity.class);
                        break;

                }
            }
        });
    }

    @CoreEvent(coreClientClass = LoginApi.class)
    public void onLogout() {
        openActivity(InitActivity.class);
        finish();
    }


    class SetAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return itemNames.length;
        }

        @Override
        public Object getItem(int position) {
            return itemNames[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(SettingActivity.this).inflate(R.layout.item_setting, parent, false);
            }
            TextView title = (TextView) convertView.findViewById(R.id.item_set_title);
            title.setText(itemNames[position]);
            return convertView;
        }
    }
}
